﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Linq;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace WindowsFormsApp2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            listView1.View = View.Details;
            listView1.Columns.Add("单词名称:", 150, HorizontalAlignment.Left);
            listView1.Columns.Add("数量:", 150, HorizontalAlignment.Left);
            clear();
        }
        public void addData(string[] name, string[] num) 
        {          
            listView1.BeginUpdate();
            for (int i = 0; i < num.Length; i++)
            {
                var item = new ListViewItem();
                item.Text = name[i]; //本地路径
                item.SubItems.Add(num[i]); //执行状态
                listView1.Items.Add(item);
                listView1.Items[listView1.Items.Count - 1].EnsureVisible();//滚动到最后
            }
            listView1.EndUpdate();
        }
        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {           
        }
        private void setNums(int originalLineCount, int originalWordCount, int formattedLineCount, int formattedWordCount)
        {
            label1.Text = "原始行数    : " + originalLineCount;
            label2.Text = "格式化行数  : " + formattedLineCount;
            label3.Text = "原始单词数  : " + originalWordCount;
            label4.Text = "格式化单词数: " + formattedWordCount;
        }
        private void button1_Click(object sender, EventArgs e)
        {
            FileAnalyzer.setNum += setNums;
            try
            {
                string name = textBox1.Text;
                string[] words;
                string[] nums;
                FileAnalyzer.AnalyzeFile(name, out words, out nums);
                addData(words, nums);
            }catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private void clear()
        {
            listView1.Items.Clear();
            label1.Text = "";
            label2.Text = "";
            label3.Text = "";
            label4.Text = "";
            textBox1.Text = "";
        }
        private void button2_Click(object sender, EventArgs e)
        {
            clear();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog
            {
                Filter = "C# 文件 (*.cs)|*.cs",
                Title = "选择一个 .cs 文件"
            };

            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                string selectedFilePath = openFileDialog.FileName;
                textBox1.Text = selectedFilePath;
            }
            else
            {
               // Console.WriteLine("用户取消了文件选择。");
            }
        }
    }
}
